package com.powernode.teatraceability.controller.contract;


import com.powernode.teatraceability.dao.depositevidence.EntCerInfoMapper;
import com.powernode.teatraceability.dao.depositevidence.ProCerInfoMapper;
import com.powernode.teatraceability.pojo.login.User;
import com.powernode.teatraceability.service.basicinfomgt.EnterpriseService;
import com.powernode.teatraceability.service.contract.ProductCertificateService;
import com.powernode.teatraceability.service.eviinfo.ProEviService;
import com.powernode.teatraceability.utils.GlobalConfig;
import com.powernode.teatraceability.utils.JsonData;
import com.powernode.teatraceability.vo.ProductCertificateVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;

/**
 * ClassName: ProductCertificatesController
 * Description:
 *
 * @Author: jdz
 * @Create: 2023/4/27 - 15:40
 */

@RestController
@RequestMapping("/procertificate")
public class  ProductCertificatesController {
    @Resource
    private ProductCertificateService service;
    @Value("${system.account.address}")
    private String accountAddress;
    @Resource
    private ProCerInfoMapper proMapper;
    @Resource
    private EnterpriseService enterpriseService;
    @Resource
    private ProEviService proEviService;
    @PostMapping("/apply")
    public JsonData<String> applyProductCertificate(@RequestBody ProductCertificateVo inputBO,
                                                    @SessionAttribute(name = "user", required = true) User user) {
        inputBO.setQymc(enterpriseService.selectQYMCByQydm(user.getQydm()));
        System.out.println(inputBO);
        /**
         * 存数据库的数据
         */
        final int insert = proMapper.insert(inputBO);
        if (insert <= 0) {
            return JsonData.failure(401, "false", "申请上链失败");
        }
        /**
         * 要存在区块链的数据
         */

        final boolean result = service.applyProductCertificate(
                accountAddress,
                inputBO.getCpmc(),
                inputBO.getCertificateType(),
                inputBO.getCertificateMaterial(),
                inputBO.getQymc());
        if (result) {
            return JsonData.success(null, "申请上链成功！");
        } else {
            return JsonData.failure(401, null, "申请上链失败！");
        }
    }
    @GetMapping("/info/{index}")
    public JsonData<ProductCertificateVo> getProductCertificates(@PathVariable("index") String index) {
        System.out.println(index);
        final ProductCertificateVo productCertificateVo = service.getProductCertificate(accountAddress, new BigInteger(index));
        if (productCertificateVo != null) {
            return JsonData.success(productCertificateVo);
        } else {
            return JsonData.failure(401, null);
        }
    }
    @PutMapping("/update")
    public JsonData<String> modifyProductCertificate(@RequestBody ProductCertificateVo inputBO) {

        final int update = proMapper.update(inputBO);
        if (update <= 0) {
            return JsonData.failure(401, null, "修改上链信息失败!");
        }
        final boolean result = service.changeProductCertificate(
                accountAddress,
                new BigInteger(String.valueOf(inputBO.getProIndex())),
                inputBO.getCpmc(),
                inputBO.getCertificateType(),
                inputBO.getCertificateMaterial(),
                inputBO.getQymc());
        if (result) {
            return JsonData.success(null,"修改上链信息成功！");
        } else {
            return JsonData.failure(401, null, "修改上链信息失败!");
        }
    }
    @GetMapping ("/confirm/{index}")
    public JsonData<String> confirmProductCertificate(@PathVariable("index") String index) {
        final String result = service.approveProductCertificate(accountAddress, new BigInteger(index));
        if (result != null) {
            final int i = proMapper.updateEviInfo(true, result, index);
            if (i <= 0) {
                return JsonData.failure(401, null, "数据库更新失败！");
            }
            return JsonData.success(null, "上链成功！");
        } else {
            return JsonData.failure(401, null, "上链失败！");
        }
    }

    @GetMapping("/{pageNum}/{pageSize}")
    public JsonData<List<ProductCertificateVo>> selectAll(
                              @PathVariable int pageNum,
                              @PathVariable int pageSize,
                              @SessionAttribute User user){
        GlobalConfig.initPageRequest(pageNum,pageSize);
        final List<ProductCertificateVo> certificateVos = proEviService.selectAll(enterpriseService.selectQYMCByQydm(user.getQydm()));
        return GlobalConfig.LOCAL.get();
    }

    @DeleteMapping("/{id}")
    public JsonData delete(@PathVariable int id){
        proEviService.delete(id);
        return GlobalConfig.LOCAL.get();
    }

    @GetMapping("/cnt/{qymc}")
    public JsonData queryCnt(@PathVariable String qymc){
        return proEviService.queryCnt(qymc);
    }

    @GetMapping
    public JsonData countInfo( @SessionAttribute(name = "user", required = true) User user) {
        return JsonData.success(proEviService.countInfo(user.getQymc()));
    }

    @GetMapping("/{cpmc}/{pageNum}/{pageSize}")
    public JsonData selectLike(@PathVariable String cpmc,
                               @PathVariable int pageNum,
                               @PathVariable int pageSize){
        GlobalConfig.initPageRequest(pageNum, pageSize);
        proEviService.selectLike(cpmc);
        return GlobalConfig.LOCAL.get();
    }
    @GetMapping("/{type}")
    public JsonData countType(@PathVariable("type") String type,
                              @SessionAttribute(name = "user", required = true) User user) {
        return JsonData.success(proEviService.countCerType(type, user.getQymc()));
    }
}
